package cn.iocoder.yudao.module.nmkj.controller.admin.companyintroduction;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.nmkj.controller.admin.companyintroduction.vo.*;
import cn.iocoder.yudao.module.nmkj.dal.dataobject.companyintroduction.CompanyIntroductionDO;
import cn.iocoder.yudao.module.nmkj.service.companyintroduction.CompanyIntroductionService;

@Tag(name = "管理后台 - 公司介绍")
@RestController
@RequestMapping("/nmkj/company-introduction")
@Validated
public class CompanyIntroductionController {

    @Resource
    private CompanyIntroductionService companyIntroductionService;

    @PostMapping("/create")
    @Operation(summary = "创建公司介绍")
    @PreAuthorize("@ss.hasPermission('nmkj:company-introduction:create')")
    public CommonResult<Long> createCompanyIntroduction(@Valid @RequestBody CompanyIntroductionSaveReqVO createReqVO) {
        return success(companyIntroductionService.createCompanyIntroduction(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新公司介绍")
    @PreAuthorize("@ss.hasPermission('nmkj:company-introduction:update')")
    public CommonResult<Boolean> updateCompanyIntroduction(@Valid @RequestBody CompanyIntroductionSaveReqVO updateReqVO) {
        companyIntroductionService.updateCompanyIntroduction(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除公司介绍")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('nmkj:company-introduction:delete')")
    public CommonResult<Boolean> deleteCompanyIntroduction(@RequestParam("id") Long id) {
        companyIntroductionService.deleteCompanyIntroduction(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得公司介绍")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('nmkj:company-introduction:query')")
    public CommonResult<CompanyIntroductionRespVO> getCompanyIntroduction(@RequestParam("id") Long id) {
        CompanyIntroductionDO companyIntroduction = companyIntroductionService.getCompanyIntroduction(id);
        return success(BeanUtils.toBean(companyIntroduction, CompanyIntroductionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得公司介绍分页")
    @PreAuthorize("@ss.hasPermission('nmkj:company-introduction:query')")
    public CommonResult<PageResult<CompanyIntroductionRespVO>> getCompanyIntroductionPage(@Valid CompanyIntroductionPageReqVO pageReqVO) {
        PageResult<CompanyIntroductionDO> pageResult = companyIntroductionService.getCompanyIntroductionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CompanyIntroductionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出公司介绍 Excel")
    @PreAuthorize("@ss.hasPermission('nmkj:company-introduction:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCompanyIntroductionExcel(@Valid CompanyIntroductionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CompanyIntroductionDO> list = companyIntroductionService.getCompanyIntroductionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "公司介绍.xls", "数据", CompanyIntroductionRespVO.class,
                        BeanUtils.toBean(list, CompanyIntroductionRespVO.class));
    }

}